//
//  SCClassifyController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/30.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCClassifyController.h"
#import "SCOneClassifyCell.h"
#import "SCIntegralGoodsCell.h"
#import "SCGoodsDetailController.h"
#import "SCSearchTitleView.h"

@interface SCClassifyController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) SCSearchTitleView *searchTitleView;//搜索栏
@property (strong, nonatomic) UITableView       *tableView;
@property (strong, nonatomic) NSArray           *classifyArray;
@property (assign, nonatomic) NSInteger         classifyIndex;

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSArray          *childArray;
@property (strong, nonatomic) NSString         *cat_id;
@property (strong, nonatomic) NSString         *searchText;

@end

@implementation SCClassifyController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.lineView.hidden = NO;
    self.view.backgroundColor = BGColor;
    
    //设置头部搜索
    [self setHeadSearchView];
    //请求数据
    self.searchText = @"";
    [self requestData];
    //设置分类
    [self setUpTableView];
    //设置二级分类
    [self setUpCollectionView];
}
- (void)setHeadSearchView{
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0.5, kScreenW, 50)];
    headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headView];
    self.searchTitleView = [[SCSearchTitleView alloc]initWithFrame:CGRectMake(15, 0, kScreenW - 30, 35)];
    self.searchTitleView.centerY = headView.height/2.0f;
    self.searchTitleView.searchTextField.delegate = self;
    [headView addSubview:self.searchTitleView];
}
#pragma mark - searchTextField的代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入搜索内容"];
        return NO;
    }
    [textField resignFirstResponder];
    self.searchText = textField.text;
    [self requestGoodsData];
    return YES;
}
#pragma mark - TableView
- (void)setUpTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 51, 120, kScreenH - kTopHeight - 51) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = RGB(239, 239, 239);
    self.tableView.rowHeight = 48;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.classifyIndex = 0;
    [self.tableView registerNib:[UINib nibWithNibName:@"SCOneClassifyCell" bundle:nil] forCellReuseIdentifier:@"SCOneClassifyCellID"];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.classifyArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCOneClassifyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCOneClassifyCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    SCModel *model = self.classifyArray[indexPath.row];
    cell.titleLab.text = model.cat_name;
    if (self.classifyIndex == indexPath.row) {
        cell.lineView.hidden = NO;
        cell.backgroundColor = [UIColor whiteColor];
        cell.titleLab.textColor = RGB(231, 157, 58);
    }else{
        cell.lineView.hidden = YES;
        cell.backgroundColor = RGB(239, 239, 239);
        cell.titleLab.textColor = RGB(51, 51, 51);
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.classifyIndex != indexPath.row) {
        [self.view endEditing:YES];
        
        self.classifyIndex = indexPath.row;
        [self.tableView reloadData];
        
        SCModel *model = self.classifyArray[indexPath.row];
        self.cat_id = model.cat_id;
        self.searchText = @"";
        [SVProgressHUD showWithStatus:@"加载中..."];
        [self requestGoodsData];
    }
}
#pragma mark - CollectionView
- (void)setUpCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat W = (kScreenW-self.tableView.width - 36)/2.0f;
    layout.itemSize = CGSizeMake(W, W*79/105+50);
    layout.sectionInset = UIEdgeInsetsMake(12, 12, 12, 12);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(self.tableView.width, 51, kScreenW-self.tableView.width, kScreenH - kTopHeight - 51) collectionViewLayout:layout];
    self.collectionView.backgroundColor = BGColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"SCIntegralGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"SCIntegralGoodsCellID"];
    [self.view addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.childArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SCIntegralGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCIntegralGoodsCellID" forIndexPath:indexPath];
    cell.model = self.childArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    SCModel *model = self.childArray[indexPath.row];
    SCGoodsDetailController *vc = [[SCGoodsDetailController alloc]init];
    vc.goodsId = model.gift_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSDictionary *dict = @{@"cat_fid":self.cat_fid};
    [SVProgressHUD showWithStatus:@"加载中..."];
    
    NSString *url;
    if (self.isGiftApp == YES) {
        if ([self.type intValue] == 1) {//积分商城推荐
            url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftApp&a=App_tui_more"];
        }else{
            url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftApp&a=App_more"];
        }
    }else{
        if ([self.type intValue] == 1) {//商城推荐
            url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GoodsApp&a=App_tui_more"];
        }else{
            url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GoodsApp&a=App_more"];
        }
    }
    
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        NSLog(@"===%@",result);
        self.classifyArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
        [self.tableView reloadData];
        if (self.classifyArray.count != 0) {
            SCModel *model = self.classifyArray[0];
            self.cat_id = model.cat_id;
            [self requestGoodsData];
        }else{
            [SVProgressHUD showImage:nil status:@"暂无此分类数据"];
        }
    }];
}
- (void)requestGoodsData{
    
    NSString *type;
    NSString *url;
    if (self.isGiftApp == YES) {
        type = @"gift_name";
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GiftApp&a=App_two_category"];
    }else{
        type = @"goods_name";
        url = [MyTool splicingWithUrl:@"admin.php?g=System&c=GoodsApp&a=App_two_category"];
    }
    NSDictionary *dict = @{type:self.searchText,
                           @"type":self.type,
                           @"cat_id":self.cat_id,
                           @"size":@"10",
                           @"pagenow":@(1)};
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        //NSLog(@"====%@",result);
        [SVProgressHUD dismiss];
        self.childArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
        [self.collectionView reloadData];
    }];
}

@end
